#ifndef __SERVIC_CAMERA_HPP__
#define __SERVIC_CAMERA_HPP__

#include <atomic>
#include <unordered_map>
#include "udp_client.hpp"
#include "camera_packet.hpp"
#include "camera_protocol.hpp"
#include "utils.hpp"

namespace gimabl {

class ServiceCamera 
{
public:
    ServiceCamera();
    ~ServiceCamera();

    int32_t InitServiceCamera(ThreadMode mode,std::string serverIp,int32_t serverPort,int32_t port,int32_t timeoutMs);
    
    void StopServiceCamera();
    void sendMsg(uint8_t* buf,std::size_t size);

    /*-------------------- 基础功能 ---------------------------*/ 
    // 获取相机图像模式切换
    int32_t setDisplayMode(DisplayMode mode); 
    // 精准倍率
    int32_t setAccurateZoob(float size);
    // Dynamic倍率
    int32_t setDynamicZoob(ZoomControlMode mode,ZoomAction action);
    // 设置对焦指令
    int32_t setFocus(ZoomControlMode mode,FocusAction action,Coordinate coordinate);  // 目前验证这个没什么现象，到时候重新调整


    /*--------------拍照与录像---------------------------*/ 
    // 拍照录像模式切换
    int32_t switchCameraMode(CameraWorkMode mode);
    // 拍照
    int32_t takePhoto(PhotoMode mode,CameraCommand command,std::string path,std::string name);
    // 录像
    int32_t recordVideo(VideoMode mode,VideoControlCommand command,std::string path,std::string name);

    /*-----------------相机设置---------------------------*/ 
   // 录像分辨率选择
    int32_t setVisibleRecodeResolution(VideoResolution command,ResolutionType type);

    // 拍照分辨率选择
    int32_t setVisiblePitureResolution( PhotoResolutionSetting command,ResolutionType type);

    // 预览流分辨率选择
    int32_t setVisibleVideoResolution(VideoOutputMode mode,VideoResolutionFpsSetting command);

    // 视频输出编码格式 
    int32_t setVideoEncodingFormat(VideoEncodingFormat format);

    // 拍照类型选择，连续拍，延时拍等
    int32_t setTakePhotoType(TakePhotoType command,int32_t time,int32_t num);

    // 照片存储设置
    int32_t setPhotoSaveMode(PhotoFlags command);

    // 视频存储设置
    int32_t setVideoSaveMode( PhotoFlags command);

    // 自定义照片和视频存储文件夹指令
    int32_t setVideoAndPhotoSavePath( std::string path);

    // 获取自定义照片和视频存储文件夹路径
    int32_t getVideoAndPhotoSavePath();

    // 恢复出厂设置
    int32_t factoryReset(resetCommand param, ReservedCode reserved=RESERVED_DEFAULT);

    // 格式化SD卡
    int32_t formatSDCard(FormatSDCardCommand param, ReservedCode reserved=RESERVED_DEFAULT);

/*-------------------- 其他功能 ---------------------------*/ 
    // 获取相机可见参数
    int32_t getAllVisibleParams(CameraType command);     // 验证通过
    //  设置保存图片格式
    int32_t setVisiblePitureSaveFormat(ImageSaveFormat command); // 验证通过
    // 设置录播视频格式
    int32_t setVisibleRecodeSaveFormat(VideoSaveFormat command);  // 验证通过
    // 设置预览流分辨率码率
    int32_t setVisibleVideoResolutionBitrate(VideoResolutionBitrate command); // 验证通过

    // 设置开启相机数据上报
    int32_t setCameraReproSwitch(ReproSwitchMode command); // 验证通过

    // 获取相机版本号
    int32_t getCameraVersion(DevicesType type); // 验证通过

private:
    int32_t UdpReadMsgHandler(uint8_t* buf,std::size_t size);
    void ReportMsgHandler();

    bool m_exit;
    bool m_isRunning;
    std::atomic<uint32_t> counter; // 计数器

    std::shared_ptr<UdpClient> m_udpClient;
    std::thread m_reportMsgHandlerThread;

    std::mutex m_ackMsgMutex;
    std::unordered_map<uint16_t, std::unique_ptr<CameraPacket>> m_ackMsgMap;

    std::mutex m_repMsgMutex;
    std::queue<std::unique_ptr<CameraPacket>> m_repMsgQueue;
};



}  // namespace gimabl

#endif // __SERVIC_CAMERA_HPP__